import asyncio
from os import name
from pyppeteer import launch

 
"""
1. 主页 作者名 描述 关注 粉丝 简介 
2. 微头条-> Cards  作者Id  链接 内容  展现 赞 评论 时间 话题
2. Card：具体时间，图片
"""



async def main():
    browser = await launch(headless=False, ignoreDefaultArgs=['--enable-automation'], dumpio= True)
    page = await browser.newPage()
 
    # 设置页面视图大小
    await page.setViewport(viewport={'width': 1280, 'height': 800})
 
    # 是否启用JS，enabled设为False，则无渲染效果
    await page.setJavaScriptEnabled(enabled=True)
    # 超时间见 1000 毫秒
    url = "https://www.toutiao.com/c/user/token/MS4wLjABAAAAR6LbwIvNZ1ldbtMk68LUh-rDoK1mhz0tA7BEYkydUpU/?tab=weitoutiao"
    res = await page.goto(url, options={'timeout': 3000})
    resp_headers = res.headers  # 响应头
    resp_status = res.status  # 响应状态

    # 等待
    await asyncio.sleep(2)

    
    name_element = await page.querySelector('.profile-header .title .name')
    name = await (await name_element.getProperty('textContent')).jsonValue()
    print(name)

    des_element = await page.querySelector('.profile-header .des')
    des = await (await des_element.getProperty('textContent')).jsonValue()
    print(des)

    following_element = await page.querySelector('.relation dt a h3')
    following = await (await following_element.getProperty('textContent')).jsonValue()
    print(following)

    followed_element = await page.querySelector('.relation dt a p')
    followed = await (await followed_element.getProperty('textContent')).jsonValue()
    print(followed)  

    intro_element = await page.querySelector('.intro-info')
    intro = await (await intro_element.getProperty('textContent')).jsonValue()
    print(intro)  

    # 第二种方法，在while循环里强行查询某元素进行等待
    while not await page.querySelector('.weitoutiao-card'):
        pass

    await page.evaluate('_ => {window.scrollBy(0, window.innerHeight);}')
    await asyncio.sleep(3)
    cards = await page.querySelectorAll(".weitoutiao-card")
    print(len(cards))

    await page.evaluate('_ => {window.scrollBy(0, window.innerHeight);}')
    await asyncio.sleep(3)
    cards = await page.querySelectorAll(".weitoutiao-card")
    print(len(cards))
    await page.evaluate('_ => {window.scrollBy(0, window.innerHeight);}')
    await asyncio.sleep(3)
    cards = await page.querySelectorAll(".weitoutiao-card")
    print(len(cards))

    await asyncio.sleep(30)

    cards = await page.querySelectorAll(".weitoutiao-card")
    
    for i,card in enumerate(cards):
        card_content = await card.J(".ugc-content a")
        card_url = await (await card_content.getProperty('href')).jsonValue()
        card_content_str = await (await card_content.getProperty('textContent')).jsonValue()
        card_footer = await card.J(".wtt-footer .y-left") # 这里直接就是全文啊！！！
        card_footer_str = await (await card_footer.getProperty('textContent')).jsonValue()
        print(i)
        print(card_url)
        print(card_content_str)
        print(card_footer_str)
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())